const { DataTypes } = require('sequelize')

module.exports = (sequelize) => {
  const InventoryLog = sequelize.define('InventoryLog', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    product_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '产品ID'
    },
    type: {
      type: DataTypes.ENUM('in', 'out', 'adjust', 'transfer', 'count'),
      allowNull: false,
      comment: '操作类型'
    },
    operation: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: '具体操作'
    },
    quantity_before: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '操作前数量'
    },
    quantity_change: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '变更数量'
    },
    quantity_after: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '操作后数量'
    },
    unit_cost: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: '单位成本'
    },
    total_cost: {
      type: DataTypes.DECIMAL(12, 2),
      allowNull: true,
      comment: '总成本'
    },
    reference_type: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '关联类型'
    },
    reference_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '关联ID'
    },
    warehouse_location: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '仓库位置'
    },
    notes: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '备注'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '操作者ID'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  }, {
    tableName: 'inventory_logs',
    timestamps: false,
    comment: '库存日志表',
    indexes: [
      {
        fields: ['product_id']
      },
      {
        fields: ['type']
      },
      {
        fields: ['created_at']
      },
      {
        fields: ['reference_type', 'reference_id']
      }
    ]
  })

  return InventoryLog
}